package org.enlongWu.controller;

import org.enlongWu.pojo.*;
import org.enlongWu.service.ColdStorageService;
import org.enlongWu.service.ColdStorageStateService;
import org.enlongWu.service.SensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/sensor")
public class SensorController {

    @Autowired
    private SensorService sensorService;


    // 创建（Create）
    @PostMapping
    public Result<Sensor> createSensor(@RequestBody Sensor sensor) {
        System.out.println(sensor);
        Integer create = sensorService.create(sensor);
        if (create == 1){
            return Result.success(null,"成功创建了"+create+"个传感器");
        }else {
            return Result.error("创建失败");
        }
    }

    //所有传感器，条件分页
    @GetMapping("/list")
    public Result<PageBean<Sensor>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String searchString
    ) {
        PageBean<Sensor> pb =  sensorService.list(pageNum,pageSize,searchString);
        return Result.success(pb);
    }

    /**
     * 修改传感器属性：
     * 也可以用于修改
     * @param sensor
     * @return
     */
    @PutMapping("/update")
    public Result bindSensor(
            @RequestBody Sensor sensor
    ){
        if (sensorService.update(sensor) == 1){
            return Result.success(null,"成功更新传感器");
        }else return Result.error("传感器更新失败");
    }
}
